//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommunityServer.Blogs.Components;

namespace CommunityServer.Blogs.Controls
{
	/// <summary>
	/// Summary description for CategoryTagControl.
	/// </summary>
	public class CategoryTagControl : WeblogThemedControl
	{
		public CategoryTagControl()
		{
	
		}

		private bool _linkToTagBrowswer = true;
		public bool LinkToTagBrowser
		{
			get
			{
				return _linkToTagBrowswer;
			}
			set
			{
				_linkToTagBrowswer = value;
			}
		}

        private Repeater categories;

        private ArrayList _data;
        public ArrayList DataSource
        {
            get
            {
                return _data;
            }
            set
            {
                _data = value;
            }
        }

        public override void DataBind()
        {
            base.DataBind ();

            if(categories != null && DataSource != null && DataSource.Count > 0)
            {
                categories.DataSource = DataSource;
                categories.DataBind();
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender (e);

            if(categories == null || DataSource == null || DataSource.Count == 0)
                this.Visible = false;
        }

        /// <summary>
	    /// Override this method to attach templated or external skin controls to local references.
	    /// </summary>
	    /// <remarks>
	    /// This will only be called if the non-default skin is used.
	    /// </remarks>
	    protected override void AttachChildControls()
	    {
	        categories = FindControl("Categories") as Repeater;
            if(categories != null)
                categories.ItemDataBound +=new RepeaterItemEventHandler(categories_ItemDataBound);
        }

        private void categories_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                HyperLink link = e.Item.FindControl("Link") as HyperLink;
                if(link != null)
                {
                    CategoryTag tag = e.Item.DataItem as CategoryTag;
                    if(tag != null)
                    {
                        link.Text = tag.Name;

						if (this.LinkToTagBrowser)
							link.NavigateUrl = BlogUrls.Instance().TagsBrowser(this.CurrentWeblog.ApplicationKey, new string[] {tag.Name});
						else
							link.NavigateUrl = BlogUrls.Instance().IndividualCategoryPage(this.CurrentWeblog.ApplicationKey, tag.CategoryID);

                        link.Attributes.Add("rel","tag");
                        
                    }
                }
            }
        }
    }
}
